<?php
#!/usr/bin/php
# $id$

require_once('config.inc.php');
require_once('DBAccessModel.class.php');
require_once('AppController.class.php');

$db = new DBAccess('recieved');
$controller = new AppController($db);

#%p the phone number of the sender of the SMS message
#%a all words of the SMS message
#%C message charset
#%i the smsc-id of the connection that received the message

# received.php %p %a %C %i
#               1 2  3  4


# insert to database recieved sms
#$d['body']    = trim(urldecode($argv[4]));
#$d['msisdn'] = $db->num_format(urldecode($argv[1]));
#$d['smsc']    = urldecode($argv[3]);
#$d['charset'] = urldecode($argv[2]);

$d['body']    = $_REQUEST['body'];
$d['msisdn'] = $db->num_format($_REQUEST['msisdn']);
$d['smsc']    = $_REQUEST['smsc'];
$d['charset'] = $_REQUEST['charset'];
$db->save_messages($d);

# pattern to check for possible command case insensitive
$pattern = '/^([\d\w-_]+)(.*)/i';

if (preg_match($pattern, $d['body'], $m)) {
    $command = $db->get_pattern($m[1]); #$db->num2txt($m[0]);
	$d['message'] = trim($m[2]);
} else {
    exit();
}
echo $command;
switch ($command)
{
    case 'grpmsg':
		$controller->group_message($d);
    break;

    case 'balance':
	$controller->balance($d);
        break;
    case 'balance2':
       $db->send_sms($d['msisdn'], $db->get_tmpl('balance3'), $d['smsc']);
	break;
    case 'help':
       $db->send_sms($d['msisdn'], $db->get_tmpl('help'), $d['smsc']);
    break;

    default:
//       $db->send_sms($d['msisdn'], $db->get_tmpl('invalid'), $d['smsc']);
    break;
}

?>
